package com.example.actionParam;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * 行为化参数传递代码
 * Created by butterfly on 2018/6/29.
 */
public class AppleDemo {
    public static void main(String[] args) {
        List<Apple> list = new ArrayList<>();
        list.add(new Apple("红",10));
        list.add(new Apple("绿",100));
        //方案1：普通做法
        appleFilter(list,new AppleFilterPrint());
        //方案2：匿名类做法
        appleFilter(list,new AppleFilterPrint(){
            @Override
            public String print(Apple apple) {
                return apple.getWeight()+"";
            }
        });
        //方案3：下面用lambda表达式替换上面代码
//        appleFilter(list, (Apple apple) -> apple.getWeight()+"");
        appleFilter(list,apple -> {
            return apple.getWeight()+"";
        });
        //谓词复合
        Predicate predicate = o -> false;
        Predicate negate = predicate.negate();//非
        Predicate and = predicate.and(o -> true);//与
        Predicate or = predicate.or(o -> true);//或
    }

    public static void appleFilter(List<Apple> list,AppleFilter af){
        for (Apple apple : list) {
            String weight = af.print(apple);
            System.out.println("这个苹果重"+weight);
        }
    }
}
